﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace WinApp
{
    public partial class FormHowTo : Form
    {
        public FormHowTo()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadNodes();
        }

        private void FormHowTo_Resize(object sender, EventArgs e)
        {
            ResizeControls();
        }

        private void ResizeControls()
        {
            treeViewContent.Height = Size.Height - 90;
            contentBrowser.Height = Size.Height - 90;
            contentBrowser.Width = Width - treeViewContent.Width - 30;
        }

        private XmlTextReader LoadXmlFromFile(string fileName)
        {
            XmlTextReader reader = new XmlTextReader(fileName);
            reader.Read();

            return reader;
        }

        private void LoadNodes()
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(LoadXmlFromFile("DryManual.xml"));

            XmlNodeList xmlNodeList = xmlDocument.SelectNodes("//Manual/Section");
            for (int i = 0; i < xmlNodeList.Count; i++)
            {
                TreeNode sectionNode = new TreeNode();
                sectionNode.Text = xmlNodeList.Item(i).Attributes["Name"].InnerText;
                sectionNode.Tag = CreateCompleteHtmlCode(xmlNodeList.Item(i).SelectSingleNode("Html").FirstChild.InnerText);

                LoadNodes(sectionNode, xmlNodeList.Item(i));

                treeViewContent.Nodes.Add(sectionNode);
            }

            if (treeViewContent.Nodes.Count > 0)
                contentBrowser.DocumentText = treeViewContent.Nodes[0].Tag.ToString();

            treeViewContent.ExpandAll();
        }

        private void LoadNodes(TreeNode sectionNode, XmlNode xmlNode)
        {
            XmlNodeList xmlNodeList = xmlNode.SelectNodes("Section");
            for (int i = 0; i < xmlNodeList.Count; i++)
            {
                TreeNode subSectionNode = new TreeNode();
                subSectionNode.Text = xmlNodeList.Item(i).Attributes["Name"].InnerText;
                subSectionNode.Tag = CreateCompleteHtmlCode(xmlNodeList.Item(i).SelectSingleNode("Html").FirstChild.InnerText);

                LoadNodes(subSectionNode, xmlNodeList.Item(i));

                sectionNode.Nodes.Add(subSectionNode);
            }
        }

        private string GetStylesheet()
        {
            return @"<style>
                         * { padding:0px 0px 0px 0px; margin:0px 0px 0px 0px; }

                         html { font-size:11px; font-family:verdana;}
                         body { padding:5px 5px 5px 5px; }

                         h1 { font-size:18px; margin-bottom:15px; }
                         h2 { font-size:14px; margin-bottom:10px; }
                         h3 { font-size:12px; margin-bottom:5px; }
                         h4 { font-size:10px; margin-bottom:5px; }

                         p { margin:0px 0px 10px 0px;  }

                         .code      { font-family:courier new; line-height:18px; font-size:12px; }
                         .indent    { padding:0px 0px 0px 20px; }
                         .comment   { color:green; }
                     </style>";
        }

        private string CreateCompleteHtmlCode(string content)
        {
            return @"<html>
                        <header>
                        </header>
                        <body>"
                            + GetStylesheet() + content + 
                        @"</body>
                    </html>";
        }

        private void treeViewContent_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            contentBrowser.DocumentText = e.Node.Tag.ToString();
            this.Text = "Dry Code Generator - How Do I - " + e.Node.Text;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}